# Maintainer: Rafal Brzegowy <rafal.brzegowy@yahoo.com>

_realname=opencascade
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=7.5.3
_pkgver2=V${pkgver//./_}
pkgrel=1
pkgdesc='Open CASCADE Technology, 3D modeling & numerical simulation (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
depends=("${MINGW_PACKAGE_PREFIX}-tk"
         "${MINGW_PACKAGE_PREFIX}-tcl"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-ffmpeg"
         "${MINGW_PACKAGE_PREFIX}-rapidjson"
         "${MINGW_PACKAGE_PREFIX}-openvr"
         $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo \
           "${MINGW_PACKAGE_PREFIX}-intel-tbb" \
           "${MINGW_PACKAGE_PREFIX}-vtk" \
           "${MINGW_PACKAGE_PREFIX}-freeimage")
         )
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-qt5-base"
             "${MINGW_PACKAGE_PREFIX}-doxygen"
             "${MINGW_PACKAGE_PREFIX}-cc")
license=('custom')
url='https://www.opencascade.org'
source=("opencascade-${pkgver}.tgz::https://git.dev.opencascade.org/gitweb/?p=occt.git;a=snapshot;h=refs/tags/${_pkgver2};sf=tgz"
        "0001-fix-compile-openvr.patch"

        # The following patches are ported from the Debian Science Team
        # https://salsa.debian.org/science-team/opencascade/-/tree/master/debian/patches

        # Source: https://salsa.debian.org/science-team/opencascade/-/blob/master/debian/patches/0002-GeomPlate_BuildAveragePlane-BasePlan-Don-t-set-yvect.patch
        "0002-GeomPlate_BuildAveragePlane-BasePlan-Don-t-set-yvect.patch"

        # Source: https://salsa.debian.org/science-team/opencascade/-/blob/master/debian/patches/0003-BRepFill_Filling-WireFromList-We-can-t-assume-that-a.patch
        "0003-BRepFill_Filling-WireFromList-We-can-t-assume-that-a.patch"

        # Source: https://salsa.debian.org/science-team/opencascade/-/blob/master/debian/patches/0004-BRepFill_Filling-Curve-constraints-confused-by-impli.patch
        "0004-BRepFill_Filling-Curve-constraints-confused-by-impli.patch"

        # Source: https://salsa.debian.org/science-team/opencascade/-/blob/master/debian/patches/0005-BRepFill_Filling-Don-t-even-attempt-to-build-with-em.patch
        "0005-BRepFill_Filling-Don-t-even-attempt-to-build-with-em.patch"

        # Source: https://salsa.debian.org/science-team/opencascade/-/blob/master/debian/patches/0006-BRepOffset_Tool-TryProject-Check-return-of-BRepLib-B.patch
        "0006-BRepOffset_Tool-TryProject-Check-return-of-BRepLib-B.patch"

        # Source: https://salsa.debian.org/science-team/opencascade/-/blob/master/debian/patches/0007-ChFi3d_Builder-PerformIntersectionAtEnd-Check-max-nu.patch
        "0007-ChFi3d_Builder-PerformIntersectionAtEnd-Check-max-nu.patch"

        # Source: http://git.dev.opencascade.org/gitweb/?p=occt.git;a=commitdiff;h=329e5df986519164083d78b28ae62d1a3b10fa6a
        "0008-Add-error-checking-to-chamfer-and-fillet-code.patch" # 0008 is scheduled for inclusion in OCCT 7.6.0

        "0009-sse2-detetction-gcc.patch")

sha256sums=('SKIP'
            '0bbba5858e3e478759dac3d4802f821ec633304a63aeef542e0e4e0e0674e205'
            '9e72d389359899d959fa66b131c9871c15a136bb600c246fcf7e05910d277335'
            'f126465de547d606c4af2e95bd550ba7d5528d53a9b73693078952f9a40b3cae'
            'b62b1c5195254d5f80d913f0f5a6a009638f0aa697e2a84da58e3cf19bf0c958'
            'cfffe467050af56c65e3d6bd1827041661c9e29858b7c83089427f2905a078b2'
            'd39ce849914e6349161675b9d412351684eb6af4bed1aa31afbdc54fa153a532'
            '148a70486a83328fc26845549fa3f175533c481525e26ce0079c8a8e0eab66fa'
            '50a0b216da456c6add6a20d411b9dced18e9905e42738609ecca72099992c8e4'
            '3bc45477d80f32626a301fc714e8060e08697ff3fc8ac5c10e3fdc70c77000f0')

prepare() {
  cd "${srcdir}"/occt-${_pkgver2}
  patch -p1 -i "${srcdir}"/0001-fix-compile-openvr.patch
  patch -p1 -i "${srcdir}"/0002-GeomPlate_BuildAveragePlane-BasePlan-Don-t-set-yvect.patch
  patch -p1 -i "${srcdir}"/0003-BRepFill_Filling-WireFromList-We-can-t-assume-that-a.patch
  patch -p1 -i "${srcdir}"/0004-BRepFill_Filling-Curve-constraints-confused-by-impli.patch
  patch -p1 -i "${srcdir}"/0005-BRepFill_Filling-Don-t-even-attempt-to-build-with-em.patch
  patch -p1 -i "${srcdir}"/0006-BRepOffset_Tool-TryProject-Check-return-of-BRepLib-B.patch
  patch -p1 -i "${srcdir}"/0007-ChFi3d_Builder-PerformIntersectionAtEnd-Check-max-nu.patch
  patch -p1 -i "${srcdir}"/0008-Add-error-checking-to-chamfer-and-fillet-code.patch
  patch -p1 -i "${srcdir}"/0009-sse2-detetction-gcc.patch
}

build() {
  local common_config
  common_config=(
    -DUSE_D3D=1
    -D3RDPARTY_DIR="${MINGW_PREFIX}"
    -D3RDPARTY_TK_DIR="${MINGW_PREFIX}"
    -D3RDPARTY_TK_DLL_DIR="${MINGW_PREFIX}/bin/"
    -D3RDPARTY_TK_DLL="${MINGW_PREFIX}/bin/tk86.dll"
    -D3RDPARTY_TK_LIBRARY_DIR="${MINGW_PREFIX}/lib/"
    -D3RDPARTY_TCL_DIR="${MINGW_PREFIX}"
    -D3RDPARTY_TCL_DLL_DIR="${MINGW_PREFIX}/bin/"
    -D3RDPARTY_TCL_DLL="${MINGW_PREFIX}/bin/tcl86.dll"
    -D3RDPARTY_TCL_LIBRARY_DIR="${MINGW_PREFIX}/lib/"
    -D3RDPARTY_FREETYPE_DIR="${MINGW_PREFIX}"
    -D3RDPARTY_FREETYPE_DLL_DIR="${MINGW_PREFIX}/bin/"
    -D3RDPARTY_FREETYPE_DLL="${MINGW_PREFIX}/bin/libfreetype-6.dll"
    -D3RDPARTY_FREETYPE_LIBRARY_DIR="${MINGW_PREFIX}/lib/"
    -DUSE_FFMPEG=1
    -D3RDPARTY_FFMPEG_DIR="${MINGW_PREFIX}"
    -D3RDPARTY_FFMPEG_DLL_DIR_avformat="${MINGW_PREFIX}/bin/"
    -D3RDPARTY_FFMPEG_DLL_avformat="${MINGW_PREFIX}/bin/avformat-58.dll"
    -D3RDPARTY_FFMPEG_LIBRARY_DIR_avformat="${MINGW_PREFIX}/lib/"
    -D3RDPARTY_FFMPEG_DLL_DIR_avutil="${MINGW_PREFIX}/bin/"
    -D3RDPARTY_FFMPEG_DLL_avutil="${MINGW_PREFIX}/bin/avutil-56.dll"
    -D3RDPARTY_FFMPEG_LIBRARY_DIR_avutil="${MINGW_PREFIX}/lib/"
    -D3RDPARTY_FFMPEG_DLL_DIR_avcodec="${MINGW_PREFIX}/bin/"
    -D3RDPARTY_FFMPEG_DLL_avcodec="${MINGW_PREFIX}/bin/avcodec-58.dll"
    -D3RDPARTY_FFMPEG_LIBRARY_DIR_avcodec="${MINGW_PREFIX}/lib/"
    -D3RDPARTY_FFMPEG_DLL_DIR_swscale="${MINGW_PREFIX}/bin/"
    -D3RDPARTY_FFMPEG_DLL_swscale="${MINGW_PREFIX}/bin/swscale-5.dll"
    -D3RDPARTY_FFMPEG_LIBRARY_DIR_swscale="${MINGW_PREFIX}/lib"
    -DUSE_OPENVR=1
    -D3RDPARTY_OPENVR_DIR="${MINGW_PREFIX}"
    -D3RDPARTY_OPENVR_DLL_DIR_openvr_api="${MINGW_PREFIX}/bin/"
    -D3RDPARTY_OPENVR_DLL_openvr_api="${MINGW_PREFIX}/bin/libopenvr_api.dll"
    -D3RDPARTY_OPENVR_LIBRARY_DIR_openvr_api="${MINGW_PREFIX}/lib/"
    -DUSE_RAPIDJSON=1
    -D3RDPARTY_RAPIDJSON_DIR="${MINGW_PREFIX}"
  )
  if [[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]]; then
    common_config+=(
      -DUSE_FREEIMAGE=1
      -D3RDPARTY_FREEIMAGE_DIR="${MINGW_PREFIX}"
      -D3RDPARTY_FREEIMAGE_DLL_DIR_freeimage="${MINGW_PREFIX}/bin/"
      -D3RDPARTY_FREEIMAGE_DLL_freeimage="${MINGW_PREFIX}/bin/libfreeimage-3.dll"
      -D3RDPARTY_FREEIMAGE_LIBRARY_DIR_freeimage="${MINGW_PREFIX}/lib/"
      -DUSE_TBB=1
      -D3RDPARTY_TBB_DLL_DIR="${MINGW_PREFIX}/bin/"
      -D3RDPARTY_TBB_DLL="${MINGW_PREFIX}/bin/tbb.dll"
      -D3RDPARTY_TBB_LIBRARY_DIR="${MINGW_PREFIX}/lib/"
      -D3RDPARTY_TBB_LIBRARY="${MINGW_PREFIX}/lib/libtbb.dll.a"
      -D3RDPARTY_TBBMALLOC_DLL_DIR="${MINGW_PREFIX}/bin/"
      -D3RDPARTY_TBBMALLOC_DLL="${MINGW_PREFIX}/bin/tbbmalloc.dll"
      -D3RDPARTY_TBBMALLOC_LIBRARY_DIR="${MINGW_PREFIX}/lib/"
      -D3RDPARTY_TBBMALLOC_LIBRARY="${MINGW_PREFIX}/lib/libtbbmalloc.dll.a"
      -DUSE_VTK=1
    )
  fi

  #Static Build
  [[ -d "${srcdir}/build-${MSYSTEM}-static" ]] && rm -rf "${srcdir}/build-${MSYSTEM}-static"
  mkdir -p "${srcdir}/build-${MSYSTEM}-static" && cd "${srcdir}/build-${MSYSTEM}-static"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -G"MSYS Makefiles" \
    -DCMAKE_BUILD_TYPE="Release" \
    -DINSTALL_DIR_LAYOUT="Unix" \
    -DBUILD_LIBRARY_TYPE="Static" \
    "${common_config[@]}" \
    -D3RDPARTY_TK_LIBRARY="${MINGW_PREFIX}/lib/libtk86.a" \
    -D3RDPARTY_TCL_LIBRARY="${MINGW_PREFIX}/lib/libtcl86.a" \
    -D3RDPARTY_FREETYPE_LIBRARY="${MINGW_PREFIX}/lib/libfreetype.a" \
    -D3RDPARTY_FFMPEG_LIBRARY_avformat="${MINGW_PREFIX}/lib/libavformat.a" \
    -D3RDPARTY_FFMPEG_LIBRARY_avutil="${MINGW_PREFIX}/lib/libavutil.a" \
    -D3RDPARTY_FFMPEG_LIBRARY_avcodec="${MINGW_PREFIX}/lib/libavcodec.a" \
    -D3RDPARTY_FFMPEG_LIBRARY_swscale="${MINGW_PREFIX}/lib/libswscale.a" \
    -D3RDPARTY_FREEIMAGE_LIBRARY="${MINGW_PREFIX}/lib/libfreeimage.a" \
    -D3RDPARTY_OPENVR_LIBRARY_openvr_api="${MINGW_PREFIX}/lib/libopenvr_api.a" \
    ../occt-${_pkgver2}

  ${MINGW_PREFIX}/bin/cmake --build .

  #Shared Build
  [[ -d "${srcdir}/build-${MSYSTEM}-shared" ]] && rm -rf "${srcdir}/build-${MSYSTEM}-shared"
  mkdir -p "${srcdir}/build-${MSYSTEM}-shared" && cd "${srcdir}/build-${MSYSTEM}-shared"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -G"MSYS Makefiles" \
    -DCMAKE_BUILD_TYPE="Release" \
    -DINSTALL_DIR_LAYOUT="Unix" \
    -DBUILD_LIBRARY_TYPE="Shared" \
    "${common_config[@]}" \
    -D3RDPARTY_TK_LIBRARY="${MINGW_PREFIX}/lib/libtk86.dll.a" \
    -D3RDPARTY_TCL_LIBRARY="${MINGW_PREFIX}/lib/libtcl86.dll.a" \
    -D3RDPARTY_FREETYPE_LIBRARY="${MINGW_PREFIX}/lib/libfreetype.dll.a" \
    -D3RDPARTY_FFMPEG_LIBRARY_avformat="${MINGW_PREFIX}/lib/libavformat.dll.a" \
    -D3RDPARTY_FFMPEG_LIBRARY_avutil="${MINGW_PREFIX}/lib/libavutil.dll.a" \
    -D3RDPARTY_FFMPEG_LIBRARY_avcodec="${MINGW_PREFIX}/lib/libavcodec.dll.a" \
    -D3RDPARTY_FFMPEG_LIBRARY_swscale="${MINGW_PREFIX}/lib/libswscale.dll.a" \
    -D3RDPARTY_FREEIMAGE_LIBRARY="${MINGW_PREFIX}/lib/libfreeimage.dll.a" \
    -D3RDPARTY_OPENVR_LIBRARY_openvr_api="${MINGW_PREFIX}/lib/libopenvr_api.dll.a" \
    ../occt-${_pkgver2}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  #Static Install
  cd "${srcdir}/build-${MSYSTEM}-static"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --build . --target install

  #Shared Install
  cd "${srcdir}/build-${MSYSTEM}-shared"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --build . --target install

  install -Dm644 "${srcdir}/occt-${_pkgver2}/LICENSE_LGPL_21.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"

  # Fix cmake files
  find "${pkgdir}${MINGW_PREFIX}/lib/cmake" -type f \( -name '*.cmake' \) \
      -exec sed -i -e 's|\${OCCT_INSTALL_BIN_LETTER}||g' {} \;

  find "${pkgdir}${MINGW_PREFIX}/lib/cmake" -type f \( -name '*.cmake' \) \
      -exec sed -i -e "s|$(cygpath -m "${MINGW_PREFIX}")/lib/||g" {} \;
}
